package com.bci.pwtz.controller.app;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.remote.ListResponse;
import com.bci.pwtz.common.remote.ObjResponse;
import com.bci.pwtz.common.remote.ReturnCode;
import com.bci.pwtz.common.remote.req.BaseIdReq;
import com.bci.pwtz.common.remote.req.BdzListReq;
import com.bci.pwtz.common.remote.req.BdzObjReq;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.controller.AbstractController;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Bdz;
import com.bci.pwtz.service.BdzService;
import com.bci.pwtz.task.TaskManager;
import com.bci.pwtz.task.thread.UpdateDeviceThread;
@Controller
@RequestMapping("/api/bdz")
public class BdzAppController extends AbstractController
{
@Autowired
BdzService service;

@RequestMapping(value="/list",method=RequestMethod.POST)
@ResponseBody
public ListResponse<Bdz> list(HttpServletRequest request){
    PaginationList<Bdz> list=new PaginationList<Bdz>();
    ListResponse<Bdz> resp=new ListResponse<Bdz>();
    BdzListReq req=null;
    try
    {
        req = (BdzListReq) getJsonToObj(request, BdzListReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空");
            return resp;
        }
        if(req.getTimestamp() == null || req.getCompanyId() == null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        list = service.list(req.getDeviceName(), req.getCompanyId(), PwtzConstants.PORTAL_TYPE_APP,req.getBeginTime(), null, req.getPageSize(),req.getPage());
        if (list != null)
        {
        	Thread t=new Thread(new UpdateDeviceThread(req.getCompanyId()));
//        	t.sleep(PwtzConstants.THREAD_SLEEP_TIME*60*1000);
        	t.start();
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("查询变电站信息成功");
            resp.setTimestamp(req.getTimestamp());
            resp.setObjs(list);
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("查询变电站信息调用接口返回失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public ObjResponse<Bdz> add(HttpServletRequest request)
{
    BdzObjReq req = null;
    ObjResponse<Bdz> resp=new ObjResponse<Bdz>();
    try
    {
        req = (BdzObjReq) getJsonToObj(request, BdzObjReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空错误");
            return resp;
        }
        if(req.getObj() == null || req.getTimestamp() == null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        int result=0;
        Bdz rtObj = null;
        if(service.load(((Bdz)req.getObj()).getDeviceid())!=null){
            result=service.update((Bdz)req.getObj());
        }else{
            rtObj=service.add((Bdz)req.getObj());
        }
        if (rtObj != null||result>0)
        {
            rtObj=service.load(((Bdz)req.getObj()).getDeviceid());
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("新增变电站成功");
            rtObj.setLastoperatorid(req.getUserId());
            service.update(rtObj);
            resp.setTimestamp(req.getTimestamp());
            resp.setObj(rtObj);
            setLog(rtObj.getDeviceid(), rtObj.getCompanyid(), AbstractController.低压表箱, AbstractController.add, "新增低压表箱["+rtObj.getDevicename()+"]成功", req.getUserId());
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("新增变电站调用接口返回失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}

/**
 * 
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:33:25<br/>
 * <p>
 * @param request
 * @return   
 *
 */
@RequestMapping(value = "/update", method = RequestMethod.POST)
@ResponseBody
public ObjResponse<Bdz> update(HttpServletRequest request)
{
    BdzObjReq req = null;
    ObjResponse<Bdz> resp = new ObjResponse<Bdz>();
    int result = 0;
    try
    {
        req = (BdzObjReq) getJsonToObj(request, BdzObjReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空错误");
            return resp;
        }
        if(req.getObj() == null || req.getTimestamp() == null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        result = service.update((Bdz) req.getObj());
        if (result > 0)
        {
            Bdz rtObj=service.load(((Bdz) req.getObj()).getDeviceid());
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("修改变电站成功");
            rtObj.setLastoperatorid(req.getUserId());
            service.update(rtObj);
            resp.setObj(rtObj);
            resp.setTimestamp(req.getTimestamp());
            setLog(((Bdz) req.getObj()).getDeviceid(), ((Bdz) req.getObj()).getCompanyid(), AbstractController.低压表箱, AbstractController.update, "修改低压表箱["+((Bdz) req.getObj()).getDevicename()+"]成功", req.getUserId());
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("修改变电站调用接口返回失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    System.out.println(req.getObj());
    return resp;
}

/**
 * 删除低压表箱信息
 * 一句话描述 <br/>
 * <p>Description: TODO(详细描述) 
 * <br/>
 * <p>Author: Administrator<br/>
 * <p>Date: 2014年9月25日-上午10:33:35<br/>
 * <p>
 * @param requets
 * @return   
 *
 */
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public ObjResponse<Bdz> delete(HttpServletRequest requets)
{
    BaseIdReq req =null;
    ObjResponse<Bdz> resp = new ObjResponse<Bdz>();
    int result = 0;
    try
    {
        req = (BaseIdReq) getJsonToObj(requets, BaseIdReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空错误");
            return resp;
        }
        if(req.getTimestamp() == null || req.getDeviceId() == null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        result = service.delete(req.getDeviceId());
        if (result > 0)
        {
            Bdz rtObj=service.load(req.getDeviceId());
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("删除低压表箱成功");
            rtObj.setLastoperatorid(req.getUserId());
            service.update(rtObj);
            resp.setTimestamp(req.getTimestamp());
            resp.setObj(rtObj);
            setLog(req.getDeviceId(), rtObj.getCompanyid(), AbstractController.低压表箱, AbstractController.delete, "删除低压表箱["+rtObj.getDevicename()+"]成功", req.getUserId());
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("删除低压表箱调用接口返回 失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (CanNotDeleteException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("存在依耐关系，不能删除");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}
}
